package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import com.zzyl.common.constant.TaskStatusConstants;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.utils.DateUtils;


import com.zzyl.common.utils.IDCardUtils;

import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.DoNursingTaskDto;
import com.zzyl.nursing.dto.NursingCancelDto;
import com.zzyl.nursing.dto.NursingTaskPageDto;
import com.zzyl.nursing.dto.TaskUpdateTimeDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.vo.NursingTaskDetailVo;
import com.zzyl.nursing.vo.NursingTaskVo;
import com.zzyl.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingTaskMapper;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理任务Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-25
 */
@Service
public class NursingTaskServiceImpl extends ServiceImpl<NursingTaskMapper, NursingTask> implements INursingTaskService
{
    @Autowired
    private NursingTaskMapper nursingTaskMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询护理任务
     * 
     * @param id 护理任务主键
     * @return 护理任务
     */
    @Override
    public NursingTaskDetailVo selectNursingTaskById(Long id)
    {
        NursingTask byId = getById(id);
        NursingTaskDetailVo nursingTaskDetailVo = elderMapper.selectmsgById(byId.getElderId());
        BeanUtils.copyProperties(byId, nursingTaskDetailVo);
        nursingTaskDetailVo.setUpdater(byId.getElderName());
        nursingTaskDetailVo.setNursingName(getNursingName(byId));

        nursingTaskDetailVo.setAge(IDCardUtils.getAgeByIdCard(nursingTaskDetailVo.getIdCardNo()));

        return nursingTaskDetailVo;
    }

    /**
     * 查询护理任务列表
     * 
     * @param nursingTask 护理任务
     * @return 护理任务
     */
    @Override
    public List<NursingTask> selectNursingTaskList(NursingTask nursingTask)
    {
        return nursingTaskMapper.selectNursingTaskList(nursingTask);
    }

    /**
     * 新增护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int insertNursingTask(NursingTask nursingTask)
    {
        return save(nursingTask) ? 1 : 0;
    }

    /**
     * 修改护理任务
     * 
     * @param nursingTask 护理任务
     * @return 结果
     */
    @Override
    public int updateNursingTask(NursingTask nursingTask)
    {
        return updateById(nursingTask) ? 1 : 0;
    }

    /**
     * 批量删除护理任务
     * 
     * @param ids 需要删除的护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理任务信息
     * 
     * @param id 护理任务主键
     * @return 结果
     */
    @Override
    public int deleteNursingTaskById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 分页查询
     * @param nursingTaskPageDto
     * @return
     */
    @Override
    public List<NursingTaskVo> selectPage(NursingTaskPageDto nursingTaskPageDto) {
        List<NursingTaskVo> list =new ArrayList<>();
        List<NursingTask> nursingTasks = nursingTaskMapper.selectPages(nursingTaskPageDto);
        nursingTasks.forEach(nursingTask -> {
            NursingTaskVo nursingTaskVo = new NursingTaskVo();
            BeanUtils.copyProperties(nursingTask, nursingTaskVo);
            String nursingName="";
            List<String> names = getNursingName(nursingTask);
            for(int i=0;i<names.size();i++){
                nursingName += names.get(i);
                if(i != names.size()-1){
                    nursingName += ",";
                }
            }
            nursingTaskVo.setNursingName(nursingName);
            list.add(nursingTaskVo);
        });
        return list;
    }

    /**
     * 取消
     * @param nursingCancelDto
     */
    @Override
    public void cancel(NursingCancelDto nursingCancelDto) {
        NursingTask nursingTask = new NursingTask();
        nursingTask.setId(nursingCancelDto.getTaskId());
        nursingTask.setStatus(TaskStatusConstants.TASK_CANCEL);
        nursingTask.setCancelReason(nursingCancelDto.getReason());
        updateById(nursingTask);
    }

    /**
     *  执行
     * @param doNursingTaskDto
     */
    @Override
    public void doTask(DoNursingTaskDto doNursingTaskDto) {
        NursingTask nursingTask = new NursingTask();
        //预计服务时间
        doNursingTaskDto.getEstimatedServerTime();
        //"yyyyMMdd'T'HHmmss'Z'"
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(doNursingTaskDto.getEstimatedServerTime(), formatter);
        nursingTask.setRealServerTime(dateTime);
        //任务信息
        nursingTask.setId(doNursingTaskDto.getTaskId());
        nursingTask.setMark(doNursingTaskDto.getMark());
        nursingTask.setTaskImage(doNursingTaskDto.getTaskImage());
        nursingTask.setStatus(TaskStatusConstants.TASK_COMPLETE);
        updateById(nursingTask);
    }

    /**
     * 修改任务期望时间
     * @param taskUpdateTimeDto
     */
    @Override
    public void updateTime(TaskUpdateTimeDto taskUpdateTimeDto) {
        NursingTask nursingTask=new NursingTask();
        nursingTask.setId(taskUpdateTimeDto.getTaskId());
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        nursingTask.setEstimatedServerTime(LocalDateTime.parse(taskUpdateTimeDto.getEstimatedServerTime(),dateTimeFormatter));
        updateById(nursingTask);
    }


    /**
     * 获取护理员名称
     * @param nursingTask
     * @return
     */
    public List<String> getNursingName(NursingTask nursingTask) {
        String nursingId = nursingTask.getNursingId();
        String[] split = nursingId.split(",");
        List<Long> ids= new ArrayList<>();
        List<String> names= new ArrayList<>();
        if (split.length > 1){
            for (String id : split) {
                ids.add(Long.parseLong(id));
            }
        }else{
            ids.add(Long.parseLong(split[0]));
        }
        names=sysUserMapper.selectUserByIds(ids);

        return names;
    }
}
